package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.AddressBook;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook, HttpSession session) {
        log.info("addressBook", addressBook);
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        int count = addressBookService.count(wrapper);
        //第一次添加为默认地址
        if (count == 0) {
            addressBook.setUserId((Long) session.getAttribute("user"));
            addressBook.setIsDefault(1);
            addressBookService.save(addressBook);
            return R.success("保存成功");
        }
        addressBook.setUserId((Long) session.getAttribute("user"));
        addressBookService.save(addressBook);
        return R.success("保存成功");
    }

    @GetMapping("/list")
    public R<List> list() {
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AddressBook::getUpdateTime);
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(wrapper);
        log.info("list:{}", list);
        return R.success(list);
    }

    @GetMapping("/{id}")
    public R<AddressBook> getId(@PathVariable Long id) {
        log.info("id:{}", id);
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    @PutMapping("/default")
    public R<AddressBook> defaultAdd(@RequestBody AddressBook addressBook) {
        log.info("addressBookId", addressBook);
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault, 1);
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        AddressBook lodAddDefault = addressBookService.getOne(wrapper);
        lodAddDefault.setIsDefault(0);
        addressBookService.updateById(lodAddDefault);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    @GetMapping("/default")
    public R<AddressBook> findDefault() {
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault, 1);
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        AddressBook addressBook = addressBookService.getOne(wrapper);
        return R.success(addressBook);
    }

    @DeleteMapping()
    public R<String> delete(Long ids) {
        log.info("ids:{}", ids);
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }
}
